﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ProdeManagmente.DAO;
using ProdeManagmente.Model;
using ProdeManagmente.Libs;
using DgvFilterPopup;

namespace ProdeManagmente.Forms.ABMS
{
    public partial class FrmListTournament : Form
    {
        private static FrmListTournament instance;
        private TournamenteService tournamentService;

        private FrmListTournament()
        {
            InitializeComponent();
            LoadForm();
        }

        public static FrmListTournament getFrmListTournamentInstance()
        {
            if (instance == null)
            {
                instance = new FrmListTournament();
            }
            instance.Select();
            return instance;
        }

        private void LoadForm()
        {
            List<Tournament> tournamentsList = new List<Tournament>();
            tournamentService = TournamenteService.getTournamenteServiceInstance();
            tournamentsList = tournamentService.getTournamentListFilter("");


            DataTable dataTable = new DataTable();
            dataTable.Columns.AddRange(new DataColumn[4] {
                                new DataColumn("ID",typeof(int)),
                                new DataColumn("TORNEO",typeof(string)),
                                new DataColumn("INICIO",typeof(DateTime)),
                                new DataColumn("FINAL",typeof(DateTime))});

            foreach (Tournament tournament in tournamentsList)
            {
                dataTable.Rows.Add(tournament.getId(), tournament.getName(), tournament.getStart(), tournament.getEnd());
            }

            dataGridView1.DataSource = dataTable;
            DgvFilterManager filterManager = new DgvFilterManager(dataGridView1);
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            FrmTournament frmTournament = new FrmTournament(null);
            frmTournament.ShowDialog();
            LoadForm();
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            long idTournament = Convert.ToInt64(dataGridView1.SelectedRows[0].Cells[0].Value.ToString());

            Tournament tournament = tournamentService.getTournamentById(idTournament);
            FrmTournament frmTournament = new FrmTournament(tournament);
            frmTournament.ShowDialog();
            LoadForm();
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            DialogResult result = MessageBox.Show("Desea borrar el elemento?", "Salir", MessageBoxButtons.YesNo);

            if (result == DialogResult.Yes)
            {
                try
                {
                    long idTournament = Convert.ToInt64(dataGridView1.SelectedRows[0].Cells[0].Value.ToString());
                    tournamentService.delete(idTournament);
                }
                catch (BusinessException error)
                {
                    new MensajeSalida(error).showMensaje();
                }

                LoadForm();
            }
        }

        private void FrmListTournament_FormClosed(object sender, FormClosedEventArgs e)
        {
            instance = null;
        }
    }
}
